﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tutorial_3_Create_a_Math_Quiz
{
    public partial class Form1 : Form
    {
        // Create a Random object to generate random numbers.
        Random Randomizer = new Random(DateTime.Now.Millisecond);

        // These ints will store the numbers
        // for the addition problem.
        int Addend1;
        int Addend2;

        // These ints will store the numbers for the subtraction problem.
        int Minuend;
        int Subtrahend;

        // These ints will store the numbers for the multiplication problem.
        int Multiplicand;
        int Multiplier;

        // These ints will store the numbers for the division problem.
        int Dividend;
        int Divisor;
        // This int will keep track of the time left.
        int TimeLeft;

        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Start the quiz by filling in all of the problems
        /// and starting the timer.
        /// </summary>
        public void StartTheQuiz()
        {
            // Fill in the addition problem.
            this.Addend1 = Randomizer.Next(51);
            this.Addend2 = Randomizer.Next(51);
            this.labelPlusLeft.Text = Addend1.ToString();
            this.labelPlusRight.Text = Addend2.ToString();
            this.numericUpDownSum.Value = 0;

            // Fill in the subtraction problem.
            this.Minuend = Randomizer.Next(1, 101);
            this.Subtrahend = Randomizer.Next(1, Minuend);
            this.labelMinusLeft.Text = Minuend.ToString();
            this.labelMinusRight.Text = Subtrahend.ToString();
            this.numericUpDownDifference.Value = 0;

            // Fill in the multiplication problem.
            this.Multiplicand = Randomizer.Next(2, 11);
            this.Multiplier = Randomizer.Next(2, 11);
            this.labelTimesLeft.Text = Multiplicand.ToString();
            this.labelTimesRight.Text = Multiplier.ToString();
            this.numericUpDownProduct.Value = 0;

            // Fill in the division problem.
            this.Divisor = Randomizer.Next(2, 11);
            int temporaryQuotient = Randomizer.Next(2, 11);
            this.Dividend = Divisor * temporaryQuotient;
            this.labelDividedLeft.Text = Dividend.ToString();
            this.labelDividedRight.Text = Divisor.ToString();
            this.numericUpDownQuotient.Value = 0;

            // Start the timer.
            this.TimeLeft = 30;
            this.labelTime.Text = "30 seconds";
            this.timer1.Start();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            StartTheQuiz();
            this.buttonStart.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (CheckTheAnswer())
            {
                // If the user got the answer right, stop the timer 
                // and show a MessageBox.
                this.timer1.Stop();
                MessageBox.Show("You got all the answers right!",
                                "Congratulations");
                this.buttonStart.Enabled = true;
            }
            else if (this.TimeLeft > 0)
            {
                // Decrease the time left by one second and display 
                // the new time left by updating the Time Left label.
                this.TimeLeft--;
                this.labelTime.Text = TimeLeft + " seconds";
                if (this.TimeLeft < 6) this.labelTime.BackColor = Color.Red;
            }
            else
            {
                // If the user ran out of time, stop the timer, show
                // a MessageBox, and fill in the answers.
                this.timer1.Stop();
                this.labelTime.Text = "Time's up!";
                MessageBox.Show("You didn't finish in time.", "Sorry");
                this.numericUpDownSum.Value = this.Addend1 + this.Addend2;
                this.numericUpDownDifference.Value = Minuend - Subtrahend;
                this.numericUpDownProduct.Value = Multiplicand * Multiplier;
                this.numericUpDownQuotient.Value = Dividend / Divisor;
                this.buttonStart.Enabled = true;
            }
        }
        /// <summary>
        /// Check the answer to see if the user got everything right.
        /// </summary>
        /// <returns>True if the answer's correct, false otherwise.</returns>
        private bool CheckTheAnswer()
        {
            return
                (this.Addend1 + this.Addend2 == this.numericUpDownSum.Value) &&
                (this.Minuend - this.Subtrahend == this.numericUpDownDifference.Value) &&
                (this.Multiplicand * this.Multiplier == this.numericUpDownProduct.Value) &&
                (this.Dividend / this.Divisor == this.numericUpDownQuotient.Value);
        }

        private void Answer_Enter(object sender, EventArgs e)
        {
            // Select the whole answer in the NumericUpDown control.
            NumericUpDown answerBox = sender as NumericUpDown;

            if (answerBox != null)
            {
                int lengthOfAnswer = answerBox.Value.ToString().Length;
                answerBox.Select(0, lengthOfAnswer);
            }
        }
    }
}
